//订单详情,微信支付签名,取消订单，确定收货  取消售后
import { getOrderDetail, getPayShow, getDeleOreder, getOrderFinis, setRefundCancel } from '../../config/https';
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';
const tool = require('../../config/tool.js');
import Dialog from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paytime:"",
    orderid: '',
    teamid: '',
    detaIls: [],        //订单数据
    carrier: [],        //地址数据
    goods: [],          //商品列表
    other_data: [],     //价格
    id: ""               //接收数据id
  },
  canCel(e) {  //取消订单数据
    let that = this
    let id = e.currentTarget.dataset.id;
    let teamid = e.currentTarget.dataset.teamid;
    Dialog.confirm({
      message: '确定取消订单吗？',
    }).then(() => {
      getDeleOreder({
        remark: " 不想买了",
        id: id,
      }).then(res => {
        let other_data = that.data.other_data
        other_data.status_desc = '交易关闭'
        other_data.long_status_desc = ''
        that.setData({
          other_data
        })
      }).catch(err => {
        Toast(err.message)
      })
    })
  },
  //售后
  afterSales(e) {
    let { orderid, teamid, index } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/buyingrefund/buyingrefund?orderid=' + orderid + '&teamid=' + teamid + '&index=' + index,
    })
  },
  //点击确认收货
  onCloseTo(e) { 
    let id = e.currentTarget.dataset.id;
    Dialog.confirm({
      message: '确定收取货物吗？',
    }).then(() => { //确定的时候跳转到 订单详情

      getOrderFinis({ //确认收货
        id: id
      }).then(res => {
        console.log("确认成功", res)
        Toast("确认成功")
        wx.navigateBack({
          delta: 1
        })
      }).catch(err => {
        Toast("确认失败")
      })
    })
  },
  //跳转邀请好友
  onDetaYes(e) {
    let { orderid, teamid } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/paymenstapur/paymenstapur?orderid=' + orderid + '&teamid=' + teamid,
    })
  },
  //点击去支付
  onClose(e) {
    let that = this;
    wx.showLoading({
      title: '请稍后',
    })
    let { id, teamid } = e.currentTarget.dataset
    getPayShow({ // 微信支付签名
      orderid: id,
      teamid: teamid
    }).then(res => {
      var payInfo = res.wechat
      wx.requestPayment({ //微信支付
        'timeStamp': payInfo.timeStamp, //时间戳，
        'nonceStr': payInfo.nonceStr,  //随机字符串
        'package': payInfo.package, //统一下单接口返回的 prepay_id 参数值
        'signType': payInfo.signType,  //签名算法
        'paySign': payInfo.paySign,  //签名
        'success': res => {
          Toast("支付成功")
          that.details(id, teamid)
        },
        'fail': err => {
          Toast("支付失败")
        }
      })
    }).catch(err => {
      Toast(err.message)
    })
  },
  //取消售后
  onDelAfter(e){
    let that = this
    let orderid = e.currentTarget.dataset.orderid
    let teamid = that.data.teamid
    Dialog.confirm({
      message: '您确定取消售后吗~~',
    }).then(() => { //确定的时候跳转到 订单详情
      setRefundCancel({
        orderid:orderid
      }).then(res=>{
        that.details(orderid, teamid)
      }).catch(err=>{
        console.log(err)
      })
    })
  },
  //查看路线
  onLocaton(e) {
    let { lat, lng } = e.currentTarget.dataset
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        wx.openLocation({
          latitude:lat,
          longitude: lng,
          scale: 18
        })
      }
    })
  },

  onLoad: function (options) {    /*生命周期函数--监听页面加载*/
    this.setData({ 
      orderid: options.orderid, 
      teamid: options.teamid,
      type: options.type
    }) //接收id
    this.details(options.orderid, options.teamid) //商品详情数据
  },
  //订单详情数据
  details(orderid, teamid) {
    console.log(orderid)
    getOrderDetail({
      teamid: teamid,
      orderid: orderid
    }).then(res => {
     console.log(res)
      if (res.order.paytime) {
        res.order.paytime = tool.u_formatTimestamp(res.order.paytime * 1000, { type: 'all' });
      }
      if (res.order.freight) {
        res.order.createtime = tool.u_formatTimestamp(res.order.createtime * 1000, { type: 'all' });
      }
 
      module.exports = {
        formatTimeTwo: res.order.paytime  // 时间戳转日期
      }
      this.setData({ 
        detaIls: res,
        store:res.store,
        order: res.order, 
        carrier: res.carrier, 
        goods: res.goods, 
        other_data: res.other_data, 
        refund: res.refund
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})